
/* Includes ------------------------------------------------------------------*/
#include "stm32_uart.h"
#if (UART_RECEIVE_FRAME_MODE == USE_TIM_UPDATE_RECEIVE_UART_FRAME)
#include "stm32_tim.h"
#endif
#include "user_main.h"


/* 串口数据传输结构体 */
#if (STM32_LPUART1_ENABLE == 1)
UART_Transfer_TypeDef (LPUART1_Transfer) __MEMORY_AT(LPUART1_TRANSFER_BUFFER_ADDR);
#endif
#if (STM32_UART1_ENABLE == 1)
UART_Transfer_TypeDef (UART1_Transfer) __MEMORY_AT(UART1_TRANSFER_BUFFER_ADDR);
#endif
#if (STM32_UART2_ENABLE == 1)
UART_Transfer_TypeDef (UART2_Transfer) __MEMORY_AT(UART2_TRANSFER_BUFFER_ADDR);
#endif
#if (STM32_UART3_ENABLE == 1)
UART_Transfer_TypeDef (UART3_Transfer) __MEMORY_AT(UART3_TRANSFER_BUFFER_ADDR);
#endif
#if (STM32_UART4_ENABLE == 1)
UART_Transfer_TypeDef (UART4_Transfer) __MEMORY_AT(UART4_TRANSFER_BUFFER_ADDR);
#endif
#if (STM32_UART5_ENABLE == 1)
UART_Transfer_TypeDef (UART5_Transfer) __MEMORY_AT(UART5_TRANSFER_BUFFER_ADDR);
#endif
#if (STM32_UART6_ENABLE == 1)
UART_Transfer_TypeDef (UART6_Transfer) __MEMORY_AT(UART6_TRANSFER_BUFFER_ADDR);
#endif
#if (STM32_UART7_ENABLE == 1)
UART_Transfer_TypeDef (UART7_Transfer) __MEMORY_AT(UART7_TRANSFER_BUFFER_ADDR);
#endif
#if (STM32_UART8_ENABLE == 1)
UART_Transfer_TypeDef (UART8_Transfer) __MEMORY_AT(UART8_TRANSFER_BUFFER_ADDR);
#endif



/**
  * @brief Tx Transfer completed callback.
  * @param huart UART handle.
  * @retval None
  */
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
#if (STM32_LPUART1_ENABLE == 1)
  if (huart->Instance == LPUART1)
  {
	  LPUART1_Transfer.Transfer_TX_Status = UART_TRANSFER_END;
  }
#endif
  
#if (STM32_UART1_ENABLE == 1)
  if (huart->Instance == USART1)
  {
	  UART1_Transfer.Transfer_TX_Status = UART_TRANSFER_END;
  }
#endif
  
#if (STM32_UART2_ENABLE == 1)
  if (huart->Instance == USART2)
  {
	  UART2_Transfer.Transfer_TX_Status = UART_TRANSFER_END;
  }
#endif
  
#if (STM32_UART3_ENABLE == 1)
  if (huart->Instance == USART3)
  {
	  UART3_Transfer.Transfer_TX_Status = UART_TRANSFER_END;
  }
#endif
  
#if (STM32_UART4_ENABLE == 1)  
  if (huart->Instance == UART4)
  {
	  UART4_Transfer.Transfer_TX_Status = UART_TRANSFER_END;
  }
#endif
  
#if (STM32_UART5_ENABLE == 1)
  if (huart->Instance == UART5)
  {
	  UART5_Transfer.Transfer_TX_Status = UART_TRANSFER_END;
  }
#endif
  
#if (STM32_UART6_ENABLE == 1)
  if (huart->Instance == USART6)
  {
	  UART6_Transfer.Transfer_TX_Status = UART_TRANSFER_END;
  }
#endif
  
#if (STM32_UART7_ENABLE == 1)
  if (huart->Instance == UART7)
  {
	  UART7_Transfer.Transfer_TX_Status = UART_TRANSFER_END;
  }
#endif
  
#if (STM32_UART8_ENABLE == 1)
  if (huart->Instance == UART8)
  {
	  UART8_Transfer.Transfer_TX_Status = UART_TRANSFER_END;
  }
#endif
}


/**
  * @brief  Reception Event Callback (Rx event notification called after use of advanced reception service).
  * @param  huart UART handle
  * @param  Size  Number of data available in application reception buffer (indicates a position in
  *               reception buffer until which, data are available)
  * @retval None
  */
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
#if (UART_RECEIVE_FRAME_MODE == USE_UART_DMA_RECEIVE_UART_FRAME)
  if (huart->ReceptionType == HAL_UART_RECEPTION_STANDARD)
  {
    
#if (STM32_LPUART1_ENABLE == 1)
    if (huart->Instance == LPUART1)
    {
#if (UART_MEMORY_DCACHE_ENABLE == 1)
      /* 若此片内存使能了D-Cache加速，则需要使用SCB_InvalidateDCache_by_Addr */
	    SCB_InvalidateDCache_by_Addr((uint32_t *)LPUART1_TRANSFER_BUFFER_ADDR, UART_RX_PACKAGE_SIZE * 4);
#endif
      
      /* 接收完成 */
      LPUART1_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
      
      /* 拷贝数据到接收缓存区 */
      memcpy(LPUART1_Transfer.RxData, LPUART1_Transfer.DMARxData, Size);
      LPUART1_Transfer.RxData_Len = Size;
      
      /* 开启DMA，等待下次接收 */
      HAL_UARTEx_ReceiveToIdle_DMA(huart, LPUART1_Transfer.DMARxData, UART_DATALEN_MAX);
    }
#endif
  
#if (STM32_UART1_ENABLE == 1)
    if (huart->Instance == USART1)
    {
#if (UART_MEMORY_DCACHE_ENABLE == 1)
      /* 若此片内存使能了D-Cache加速，则需要使用SCB_InvalidateDCache_by_Addr */
	    SCB_InvalidateDCache_by_Addr((uint32_t *)UART1_TRANSFER_BUFFER_ADDR, UART_RX_PACKAGE_SIZE * 4);
#endif
    
      /* 接收完成 */
      UART1_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
      
      /* 拷贝数据到接收缓存区 */
      memcpy(UART1_Transfer.RxData, UART1_Transfer.DMARxData, Size);
      UART1_Transfer.RxData_Len = Size;
      
      /* 开启DMA，等待下次接收 */
      HAL_UARTEx_ReceiveToIdle_DMA(huart, UART1_Transfer.DMARxData, UART_DATALEN_MAX);
    }
#endif
  
#if (STM32_UART2_ENABLE == 1)
    if (huart->Instance == USART2)
    {
#if (UART_MEMORY_DCACHE_ENABLE == 1)
      /* 若此片内存使能了D-Cache加速，则需要使用SCB_InvalidateDCache_by_Addr */
	    SCB_InvalidateDCache_by_Addr((uint32_t *)UART2_TRANSFER_BUFFER_ADDR, UART_RX_PACKAGE_SIZE * 4);
#endif
    
      /* 接收完成 */
      UART2_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
      
      /* 拷贝数据到接收缓存区 */
      memcpy(UART2_Transfer.RxData, UART2_Transfer.DMARxData, Size);
      UART2_Transfer.RxData_Len = Size;
      
      /* 开启DMA，等待下次接收 */
      HAL_UARTEx_ReceiveToIdle_DMA(huart, UART2_Transfer.DMARxData, UART_DATALEN_MAX);
    }
#endif
  
#if (STM32_UART3_ENABLE == 1)
    if (huart->Instance == USART3)
    {
#if (UART_MEMORY_DCACHE_ENABLE == 1)
      /* 若此片内存使能了D-Cache加速，则需要使用SCB_InvalidateDCache_by_Addr */
	    SCB_InvalidateDCache_by_Addr((uint32_t *)UART3_TRANSFER_BUFFER_ADDR, UART_RX_PACKAGE_SIZE * 4);
#endif
    
      /* 接收完成 */
      UART3_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
      
      /* 拷贝数据到接收缓存区 */
      memcpy(UART3_Transfer.RxData, UART3_Transfer.DMARxData, Size);
      UART3_Transfer.RxData_Len = Size;
      
      /* 开启DMA，等待下次接收 */
      HAL_UARTEx_ReceiveToIdle_DMA(huart, UART3_Transfer.DMARxData, UART_DATALEN_MAX);
    }
#endif
  
#if (STM32_UART4_ENABLE == 1)
    if (huart->Instance == UART4)
    {
#if (UART_MEMORY_DCACHE_ENABLE == 1)
      /* 若此片内存使能了D-Cache加速，则需要使用SCB_InvalidateDCache_by_Addr */
	    SCB_InvalidateDCache_by_Addr((uint32_t *)UART4_TRANSFER_BUFFER_ADDR, UART_RX_PACKAGE_SIZE * 4);
#endif
    
      /* 接收完成 */
      UART4_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
      
      /* 拷贝数据到接收缓存区 */
      memcpy(UART4_Transfer.RxData, UART4_Transfer.DMARxData, Size);
      UART4_Transfer.RxData_Len = Size;
      
      /* 开启DMA，等待下次接收 */
      HAL_UARTEx_ReceiveToIdle_DMA(huart, UART4_Transfer.DMARxData, UART_DATALEN_MAX);
    }
#endif
  
#if (STM32_UART5_ENABLE == 1)
    if (huart->Instance == UART5)
    {
#if (UART_MEMORY_DCACHE_ENABLE == 1)
      /* 若此片内存使能了D-Cache加速，则需要使用SCB_InvalidateDCache_by_Addr */
	    SCB_InvalidateDCache_by_Addr((uint32_t *)UART5_TRANSFER_BUFFER_ADDR, UART_RX_PACKAGE_SIZE * 4);
#endif
    
      /* 接收完成 */
      UART5_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
      
      /* 拷贝数据到接收缓存区 */
      memcpy(UART5_Transfer.RxData, UART5_Transfer.DMARxData, Size);
      UART5_Transfer.RxData_Len = Size;
      
      /* 开启DMA，等待下次接收 */
      HAL_UARTEx_ReceiveToIdle_DMA(huart, UART5_Transfer.DMARxData, UART_DATALEN_MAX);
    }
#endif

#if (STM32_UART6_ENABLE == 1)
    if (huart->Instance == USART6)
    {
#if (UART_MEMORY_DCACHE_ENABLE == 1)
      /* 若此片内存使能了D-Cache加速，则需要使用SCB_InvalidateDCache_by_Addr */
	    SCB_InvalidateDCache_by_Addr((uint32_t *)UART6_TRANSFER_BUFFER_ADDR, UART_RX_PACKAGE_SIZE * 4);
#endif
    
      /* 接收完成 */
      UART6_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
      
      /* 拷贝数据到接收缓存区 */
      memcpy(UART6_Transfer.RxData, UART6_Transfer.DMARxData, Size);
      UART6_Transfer.RxData_Len = Size;
      
      /* 开启DMA，等待下次接收 */
      HAL_UARTEx_ReceiveToIdle_DMA(huart, UART6_Transfer.DMARxData, UART_DATALEN_MAX);
    }
#endif
  
#if (STM32_UART7_ENABLE == 1)
    if (huart->Instance == UART7)
    {
#if (UART_MEMORY_DCACHE_ENABLE == 1)
      /* 若此片内存使能了D-Cache加速，则需要使用SCB_InvalidateDCache_by_Addr */
	    SCB_InvalidateDCache_by_Addr((uint32_t *)UART7_TRANSFER_BUFFER_ADDR, UART_RX_PACKAGE_SIZE * 4);
#endif
      
      /* 接收完成 */
      UART7_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
      
      /* 拷贝数据到接收缓存区 */
      memcpy(UART7_Transfer.RxData, UART7_Transfer.DMARxData, Size);
      UART7_Transfer.RxData_Len = Size;
      
      /* 开启DMA，等待下次接收 */
      HAL_UARTEx_ReceiveToIdle_DMA(huart, UART7_Transfer.DMARxData, UART_DATALEN_MAX);
    }
#endif
  
#if (STM32_UART8_ENABLE == 1)
    if (huart->Instance == UART8)
    {
#if (UART_MEMORY_DCACHE_ENABLE == 1)
      /* 若此片内存使能了D-Cache加速，则需要使用SCB_InvalidateDCache_by_Addr */
	    SCB_InvalidateDCache_by_Addr((uint32_t *)UART8_TRANSFER_BUFFER_ADDR, UART_RX_PACKAGE_SIZE * 4);
#endif
      
      /* 接收完成 */
      UART8_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
      
      /* 拷贝数据到接收缓存区 */
      memcpy(UART8_Transfer.RxData, UART8_Transfer.DMARxData, Size);
      UART8_Transfer.RxData_Len = Size;
      
      /* 开启DMA，等待下次接收 */
      HAL_UARTEx_ReceiveToIdle_DMA(huart, UART8_Transfer.DMARxData, UART_DATALEN_MAX);
    }
#endif
  }
#endif
}



/**
  * @brief  初始化UART
  * @note   无
  * @param  STM32_UART: UART_TypeDef
  * @param  huart: UART_HandleTypeDef
  * @param  BaudRate: 波特率
  * @retval 结果
  */
int32_t STM32_UART_Init(USART_TypeDef *STM32_UART, UART_HandleTypeDef *huart, uint32_t BaudRate)
{
#if (UART_RECEIVE_FRAME_MODE == USE_TIM_UPDATE_RECEIVE_UART_FRAME)
  uint32_t SysClk_Freq;
#endif

  int32_t res = HAL_OK;

  /* 结构体UART_HandleTypeDef中的Instance必须指定有硬件串口，才能使用HAL_UART_DeInit函数 */
  huart->Instance = STM32_UART;

  /* 复位串口 */
  HAL_UART_DeInit(huart);

  huart->Instance = STM32_UART;
  huart->Init.BaudRate = BaudRate;
  huart->Init.WordLength = UART_WORDLENGTH_8B;
  huart->Init.StopBits = UART_STOPBITS_1;
  huart->Init.Parity = UART_PARITY_NONE;
  huart->Init.Mode = UART_MODE_TX_RX;
  huart->Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart->Init.OverSampling = UART_OVERSAMPLING_16;
  huart->Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart->AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(huart) != HAL_OK)
  {
    res = HAL_ERROR;
  }

  /* 打开串口接收中断 */
  __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
  
  __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_RXNE);

#if (UART_RECEIVE_FRAME_MODE == USE_TIM_UPDATE_RECEIVE_UART_FRAME)
  /* 获取系统主频，单位MHz */
  SysClk_Freq = HAL_RCC_GetSysClockFreq() / 1000000;
  
  /* 配置定时器，超过10个字节的时间下没接收到数据则触发更新中断 */
#if (STM32_LPUART1_ENABLE == 1)
  if (huart->Instance == LPUART1)
  {
	  STM32_TIM_Init(TIM_PERIPHERAL_LPUART1_RX, &TIM_HANDLE_LPUART1_RX,
                     SysClk_Freq - 1, 10 * (1000000 / (BaudRate / 10)) - 1);
  }
#endif
  
#if (STM32_UART1_ENABLE == 1)
  if (huart->Instance == USART1)
  {
    STM32_TIM_Init(TIM_PERIPHERAL_UART1_RX, &TIM_HANDLE_UART1_RX,
                     SysClk_Freq - 1, 10 * (1000000 / (BaudRate / 10)) - 1);
  }
#endif
  
#if (STM32_UART2_ENABLE == 1)
  if (huart->Instance == USART2)
  {
    STM32_TIM_Init(TIM_PERIPHERAL_UART2_RX, &TIM_HANDLE_UART2_RX,
                     SysClk_Freq - 1, 10 * (1000000 / (BaudRate / 10)) - 1);
  }
#endif
  
#if (STM32_UART3_ENABLE == 1)
  if (huart->Instance == USART3)
  {
    STM32_TIM_Init(TIM_PERIPHERAL_UART3_RX, &TIM_HANDLE_UART3_RX,
                     SysClk_Freq - 1, 10 * (1000000 / (BaudRate / 10)) - 1);
  }
#endif
  
#if (STM32_UART4_ENABLE == 1)
  if (huart->Instance == UART4)
  {
    STM32_TIM_Init(TIM_PERIPHERAL_UART4_RX, &TIM_HANDLE_UART4_RX,
                     SysClk_Freq - 1, 10 * (1000000 / (BaudRate / 10)) - 1);
  }
#endif
  
#if (STM32_UART5_ENABLE == 1)
  if (huart->Instance == UART5)
  {
    STM32_TIM_Init(TIM_PERIPHERAL_UART5_RX, &TIM_HANDLE_UART5_RX,
                     SysClk_Freq - 1, 10 * (1000000 / (BaudRate / 10)) - 1);
  }
#endif

#if (STM32_UART6_ENABLE == 1)
  if (huart->Instance == USART6)
  {
    STM32_TIM_Init(TIM_PERIPHERAL_UART6_RX, &TIM_HANDLE_UART6_RX,
                     SysClk_Freq - 1, 10 * (1000000 / (BaudRate / 10)) - 1);
  }
#endif
  
#if (STM32_UART7_ENABLE == 1)
  if (huart->Instance == UART7)
  {
    STM32_TIM_Init(TIM_PERIPHERAL_UART7_RX, &TIM_HANDLE_UART7_RX,
                     SysClk_Freq - 1, 10 * (1000000 / (BaudRate / 10)) - 1);
  }
#endif
  
#if (STM32_UART8_ENABLE == 1)
  if (huart->Instance == UART8)
  {
    STM32_TIM_Init(TIM_PERIPHERAL_UART8_RX, &TIM_HANDLE_UART8_RX,
                     SysClk_Freq - 1, 10 * (1000000 / (BaudRate / 10)) - 1);
  }
#endif

#endif
  
#if (UART_RECEIVE_FRAME_MODE == USE_UART_DMA_RECEIVE_UART_FRAME)
  /* 配置串口空闲中断时，DMA接收串口一帧数据完成这个功能 */
#if (STM32_LPUART1_ENABLE == 1)
  if (huart->Instance == LPUART1)
  {
    HAL_UARTEx_ReceiveToIdle_DMA(huart, LPUART1_Transfer.DMARxData, UART_DATALEN_MAX);
  }
#endif
  
#if (STM32_UART1_ENABLE == 1)
  if (huart->Instance == USART1)
  {
    HAL_UARTEx_ReceiveToIdle_DMA(huart, UART1_Transfer.DMARxData, UART_DATALEN_MAX);
  }
#endif
  
#if (STM32_UART2_ENABLE == 1)
  if (huart->Instance == USART2)
  {
    HAL_UARTEx_ReceiveToIdle_DMA(huart, UART2_Transfer.DMARxData, UART_DATALEN_MAX);
  }
#endif
  
#if (STM32_UART3_ENABLE == 1)
  if (huart->Instance == USART3)
  {
    HAL_UARTEx_ReceiveToIdle_DMA(huart, UART3_Transfer.DMARxData, UART_DATALEN_MAX);
  }
#endif
  
#if (STM32_UART4_ENABLE == 1)
  if (huart->Instance == UART4)
  {
    HAL_UARTEx_ReceiveToIdle_DMA(huart, UART4_Transfer.DMARxData, UART_DATALEN_MAX);
  }
#endif
  
#if (STM32_UART5_ENABLE == 1)
  if (huart->Instance == UART5)
  {
    HAL_UARTEx_ReceiveToIdle_DMA(huart, UART5_Transfer.DMARxData, UART_DATALEN_MAX);
  }
#endif

#if (STM32_UART6_ENABLE == 1)
  if (huart->Instance == USART6)
  {
    HAL_UARTEx_ReceiveToIdle_DMA(huart, UART6_Transfer.DMARxData, UART_DATALEN_MAX);
  }
#endif
  
#if (STM32_UART7_ENABLE == 1)
  if (huart->Instance == UART7)
  {
    HAL_UARTEx_ReceiveToIdle_DMA(huart, UART7_Transfer.DMARxData, UART_DATALEN_MAX);
  }
#endif
  
#if (STM32_UART8_ENABLE == 1)
  if (huart->Instance == UART8)
  {
    HAL_UARTEx_ReceiveToIdle_DMA(huart, UART8_Transfer.DMARxData, UART_DATALEN_MAX);
  }
#endif
  
#endif

  /* 清空缓冲区 */
#if (STM32_LPUART1_ENABLE == 1)
  if (huart->Instance == LPUART1)
  {
    memset(&LPUART1_Transfer, 0, sizeof(UART_Transfer_TypeDef));
  }
#endif
  
#if (STM32_UART1_ENABLE == 1)
  if (huart->Instance == USART1)
  {
    memset(&UART1_Transfer, 0, sizeof(UART_Transfer_TypeDef));
  }
#endif
  
#if (STM32_UART2_ENABLE == 1)
  if (huart->Instance == USART2)
  {
    memset(&UART2_Transfer, 0, sizeof(UART_Transfer_TypeDef));
  }
#endif
  
#if (STM32_UART3_ENABLE == 1)
  if (huart->Instance == USART3)
  {
    memset(&UART3_Transfer, 0, sizeof(UART_Transfer_TypeDef));
  }
#endif
  
#if (STM32_UART4_ENABLE == 1)
  if (huart->Instance == UART4)
  {
    memset(&UART4_Transfer, 0, sizeof(UART_Transfer_TypeDef));
  }
#endif
  
#if (STM32_UART5_ENABLE == 1)
  if (huart->Instance == UART5)
  {
    memset(&UART5_Transfer, 0, sizeof(UART_Transfer_TypeDef));
  }
#endif

#if (STM32_UART6_ENABLE == 1)
  if (huart->Instance == USART6)
  {
    memset(&UART6_Transfer, 0, sizeof(UART_Transfer_TypeDef));
  }
#endif
  
#if (STM32_UART7_ENABLE == 1)
  if (huart->Instance == UART7)
  {
    memset(&UART7_Transfer, 0, sizeof(UART_Transfer_TypeDef));
  }
#endif
  
#if (STM32_UART8_ENABLE == 1)
  if (huart->Instance == UART8)
  {
    memset(&UART8_Transfer, 0, sizeof(UART_Transfer_TypeDef));
  }
#endif
  
  return res;
}


/**
  * @brief  通过UART发送一帧数据
  * @note   无
  * @param  huart: UART_HandleTypeDef
  * @param  Data: 数据缓冲区
  * @param  Length: 数据缓冲区长度
  * @retval 结果
  */
int32_t STM32_UART_TransmitData(UART_HandleTypeDef *huart, uint8_t *Data, uint16_t Length)
{
  int32_t res = 0;

#if (STM32_LPUART1_ENABLE == 1)
  if (huart->Instance == LPUART1)
  {
	  LPUART1_Transfer.Transfer_TX_Status = UART_TRANSFER_START;
  }
#endif
  
#if (STM32_UART1_ENABLE == 1)
  if (huart->Instance == USART1)
  {
	  UART1_Transfer.Transfer_TX_Status = UART_TRANSFER_START;
  }
#endif
  
#if (STM32_UART2_ENABLE == 1)
  if (huart->Instance == USART2)
  {
	  UART2_Transfer.Transfer_TX_Status = UART_TRANSFER_START;
  }
#endif
  
#if (STM32_UART3_ENABLE == 1)
  if (huart->Instance == USART3)
  {
	  UART3_Transfer.Transfer_TX_Status = UART_TRANSFER_START;
  }
#endif
  
#if (STM32_UART4_ENABLE == 1)  
  if (huart->Instance == UART4)
  {
	  UART4_Transfer.Transfer_TX_Status = UART_TRANSFER_START;
  }
#endif
  
#if (STM32_UART5_ENABLE == 1)
  if (huart->Instance == UART5)
  {
	  UART5_Transfer.Transfer_TX_Status = UART_TRANSFER_START;
  }
#endif
  
#if (STM32_UART6_ENABLE == 1)
  if (huart->Instance == USART6)
  {
	  UART6_Transfer.Transfer_TX_Status = UART_TRANSFER_START;
  }
#endif
  
#if (STM32_UART7_ENABLE == 1) 
  if (huart->Instance == UART7)
  {
	  UART7_Transfer.Transfer_TX_Status = UART_TRANSFER_START;
  }
#endif
  
#if (STM32_UART8_ENABLE == 1)
  if (huart->Instance == UART8)
  {
	  UART8_Transfer.Transfer_TX_Status = UART_TRANSFER_START;
  }
#endif
	
  res = HAL_UART_Transmit(huart, Data, Length, 10 * Length);

#if (STM32_LPUART1_ENABLE == 1)
  if (huart->Instance == LPUART1)
  {
	  LPUART1_Transfer.Transfer_TX_Status = UART_TRANSFER_END;
  }
#endif
  
#if (STM32_UART1_ENABLE == 1)
  if (huart->Instance == USART1)
  {
	  UART1_Transfer.Transfer_TX_Status = UART_TRANSFER_END;
  }
#endif
  
#if (STM32_UART2_ENABLE == 1)
  if (huart->Instance == USART2)
  {
	  UART2_Transfer.Transfer_TX_Status = UART_TRANSFER_END;
  }
#endif
  
#if (STM32_UART3_ENABLE == 1)
  if (huart->Instance == USART3)
  {
	  UART3_Transfer.Transfer_TX_Status = UART_TRANSFER_END;
  }
#endif
  
#if (STM32_UART4_ENABLE == 1)  
  if (huart->Instance == UART4)
  {
	  UART4_Transfer.Transfer_TX_Status = UART_TRANSFER_END;
  }
#endif
  
#if (STM32_UART5_ENABLE == 1)  
  if (huart->Instance == UART5)
  {
	  UART5_Transfer.Transfer_TX_Status = UART_TRANSFER_END;
  }
#endif
  
#if (STM32_UART6_ENABLE == 1)
  if (huart->Instance == USART6)
  {
	  UART6_Transfer.Transfer_TX_Status = UART_TRANSFER_END;
  }
#endif
  
#if (STM32_UART7_ENABLE == 1) 
  if (huart->Instance == UART7)
  {
	  UART7_Transfer.Transfer_TX_Status = UART_TRANSFER_END;
  }
#endif
  
#if (STM32_UART8_ENABLE == 1)
  if (huart->Instance == UART8)
  {
	  UART8_Transfer.Transfer_TX_Status = UART_TRANSFER_END;
  }
#endif
	
  return res;
}


/**
  * @brief  通过UART发送一帧数据
  * @note   DMA
  * @param  huart: UART_HandleTypeDef
  * @param  Data: 数据缓冲区
  * @param  Length: 数据缓冲区长度
  * @retval 结果
  */
int32_t STM32_UART_TransmitData_DMA(UART_HandleTypeDef *huart, uint8_t *Data, uint16_t Length)
{
  int32_t res = 0;
	
#if (STM32_LPUART1_ENABLE == 1)
  if (huart->Instance == LPUART1)
  {
    LPUART1_Transfer.Transfer_TX_Status = UART_TRANSFER_START;
    
#if (UART_MEMORY_DCACHE_ENABLE == 1)
    /* 若此片内存使能了D-Cache加速，则需要使用SCB_CleanDCache_by_Addr */
    SCB_CleanDCache_by_Addr((uint32_t *)LPUART1_TRANSFER_BUFFER_ADDR, UART_RX_PACKAGE_SIZE * 4);
#endif
  }
#endif
  
#if (STM32_UART1_ENABLE == 1)
  if (huart->Instance == USART1)
  {
    UART1_Transfer.Transfer_TX_Status = UART_TRANSFER_START;
  
#if (UART_MEMORY_DCACHE_ENABLE == 1)
    /* 若此片内存使能了D-Cache加速，则需要使用SCB_CleanDCache_by_Addr */
    SCB_CleanDCache_by_Addr((uint32_t *)UART1_TRANSFER_BUFFER_ADDR, UART_RX_PACKAGE_SIZE * 4);
#endif
  }
#endif
  
#if (STM32_UART2_ENABLE == 1)
  if (huart->Instance == USART2)
  {
    UART2_Transfer.Transfer_TX_Status = UART_TRANSFER_START;
  
#if (UART_MEMORY_DCACHE_ENABLE == 1)
    /* 若此片内存使能了D-Cache加速，则需要使用SCB_CleanDCache_by_Addr */
    SCB_CleanDCache_by_Addr((uint32_t *)UART2_TRANSFER_BUFFER_ADDR, UART_RX_PACKAGE_SIZE * 4);
#endif
  }
#endif
  
#if (STM32_UART3_ENABLE == 1)
  if (huart->Instance == USART3)
  {
    UART3_Transfer.Transfer_TX_Status = UART_TRANSFER_START;
    
#if (UART_MEMORY_DCACHE_ENABLE == 1)
    /* 若此片内存使能了D-Cache加速，则需要使用SCB_CleanDCache_by_Addr */
    SCB_CleanDCache_by_Addr((uint32_t *)UART3_TRANSFER_BUFFER_ADDR, UART_RX_PACKAGE_SIZE * 4);
#endif
  }
#endif
  
#if (STM32_UART4_ENABLE == 1)  
  if (huart->Instance == UART4)
  {
    UART4_Transfer.Transfer_TX_Status = UART_TRANSFER_START;
    
#if (UART_MEMORY_DCACHE_ENABLE == 1)
    /* 若此片内存使能了D-Cache加速，则需要使用SCB_CleanDCache_by_Addr */
    SCB_CleanDCache_by_Addr((uint32_t *)UART4_TRANSFER_BUFFER_ADDR, UART_RX_PACKAGE_SIZE * 4);
#endif
  }
#endif
  
#if (STM32_UART5_ENABLE == 1)  
  if (huart->Instance == UART5)
  {
    UART5_Transfer.Transfer_TX_Status = UART_TRANSFER_START;
    
#if (UART_MEMORY_DCACHE_ENABLE == 1)
    /* 若此片内存使能了D-Cache加速，则需要使用SCB_CleanDCache_by_Addr */
    SCB_CleanDCache_by_Addr((uint32_t *)UART5_TRANSFER_BUFFER_ADDR, UART_RX_PACKAGE_SIZE * 4);
#endif
  }
#endif
  
#if (STM32_UART6_ENABLE == 1)
  if (huart->Instance == USART6)
  {
    UART6_Transfer.Transfer_TX_Status = UART_TRANSFER_START;
    
#if (UART_MEMORY_DCACHE_ENABLE == 1)
    /* 若此片内存使能了D-Cache加速，则需要使用SCB_CleanDCache_by_Addr */
    SCB_CleanDCache_by_Addr((uint32_t *)UART6_TRANSFER_BUFFER_ADDR, UART_RX_PACKAGE_SIZE * 4);
#endif
  }
#endif
  
#if (STM32_UART7_ENABLE == 1) 
  if (huart->Instance == UART7)
  {
    UART7_Transfer.Transfer_TX_Status = UART_TRANSFER_START;
    
#if (UART_MEMORY_DCACHE_ENABLE == 1)
    /* 若此片内存使能了D-Cache加速，则需要使用SCB_CleanDCache_by_Addr */
    SCB_CleanDCache_by_Addr((uint32_t *)UART7_TRANSFER_BUFFER_ADDR, UART_RX_PACKAGE_SIZE * 4);
#endif
  }
#endif
  
#if (STM32_UART8_ENABLE == 1)
  if (huart->Instance == UART8)
  {
    UART8_Transfer.Transfer_TX_Status = UART_TRANSFER_START;
    
#if (UART_MEMORY_DCACHE_ENABLE == 1)
    /* 若此片内存使能了D-Cache加速，则需要使用SCB_CleanDCache_by_Addr */
    SCB_CleanDCache_by_Addr((uint32_t *)UART8_TRANSFER_BUFFER_ADDR, UART_RX_PACKAGE_SIZE * 4);
#endif
  }
#endif

  res = HAL_UART_Transmit_DMA(huart, Data, Length);

  return res;
}


/**
  * @brief  UART接收中断回调函数
  * @note   该函数由UART_IRQHandler调用
  * @param  huart: UART_HandleTypeDef
  * @retval 无
  */
void STM32_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
#if (UART_RECEIVE_FRAME_MODE == USE_TIM_UPDATE_RECEIVE_UART_FRAME)
  if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) == SET)
  {
    STM32_UART_RxCpltCallback_RXNE(huart);
  }
#endif

#if (UART_RECEIVE_FRAME_MODE == USE_UART_IDLE_RECEIVE_UART_FRAME)
  if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) == SET)
  {
    STM32_UART_RxCpltCallback_RXNE(huart);
  }
  if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) == SET)
  {
    STM32_UART_RxCpltCallback_IDLE(huart);
  }
#endif

  if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) == SET)
  {
    __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
  
    __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_ORE);
  }
}


/**
  * @brief  UART使用接收完成中断回调函数
  * @note   该函数由STM32_UART_RxCpltCallback调用，UART接收完成在TIM的中断里
  * @param  huart: UART_HandleTypeDef
  * @retval 无
  */
void STM32_UART_RxCpltCallback_RXNE(UART_HandleTypeDef *huart)
{
#if (STM32_LPUART1_ENABLE == 1)
  if (huart->Instance == LPUART1)
  {
    /* 读取UART接收数据寄存器中1Byte的数据 */
    LPUART1_Transfer.DMARxData[LPUART1_Transfer.DMARxData_Len] = LPUART1->RDR;
    LPUART1_Transfer.DMARxData_Len++;
    LPUART1_Transfer.Transfer_RX_Status = UART_TRANSFER_START;

    /* 接收到一包数据 */
    if (LPUART1_Transfer.DMARxData_Len == UART_RX_PACKAGE_SIZE)
    {
      LPUART1_Transfer.DMARxData_Len = 0;
      LPUART1_Transfer.Package_Data_RX_End_Flag = UART_TRANSFER_END;
    }

    /* 是否一帧数据过长 */
    if (LPUART1_Transfer.DMARxData_Len == UART_DATALEN_MAX - 1)
    {
      LPUART1_Transfer.DMARxData_Len = 0;
    }
  }
#endif

#if (STM32_UART1_ENABLE == 1)
  if (huart->Instance == USART1)
  {
    /* 读取UART接收数据寄存器中1Byte的数据 */
    UART1_Transfer.DMARxData[UART1_Transfer.DMARxData_Len] = USART1->RDR;
    UART1_Transfer.DMARxData_Len++;
    UART1_Transfer.Transfer_RX_Status = UART_TRANSFER_START;

    /* 接收到一包数据 */
    if (UART1_Transfer.DMARxData_Len == UART_RX_PACKAGE_SIZE)
    {
      UART1_Transfer.DMARxData_Len = 0;
      UART1_Transfer.Package_Data_RX_End_Flag = UART_TRANSFER_END;
    }

    /* 是否一帧数据过长 */
    if (UART1_Transfer.DMARxData_Len == UART_DATALEN_MAX - 1)
    {
      UART1_Transfer.DMARxData_Len = 0;
    }
  }
#endif

#if (STM32_UART2_ENABLE == 1)
  if (huart->Instance == USART2)
  {
    /* 读取UART接收数据寄存器中1Byte的数据 */
    UART2_Transfer.DMARxData[UART2_Transfer.DMARxData_Len] = USART2->RDR;
    UART2_Transfer.DMARxData_Len++;
    UART2_Transfer.Transfer_RX_Status = UART_TRANSFER_START;

    /* 接收到一包数据 */
    if (UART2_Transfer.DMARxData_Len == UART_RX_PACKAGE_SIZE)
    {
      UART2_Transfer.DMARxData_Len = 0;
      UART2_Transfer.Package_Data_RX_End_Flag = UART_TRANSFER_END;
    }

    /* 是否一帧数据过长 */
    if (UART2_Transfer.DMARxData_Len == UART_DATALEN_MAX - 1)
    {
      UART2_Transfer.DMARxData_Len = 0;
    }
  }
#endif

#if (STM32_UART3_ENABLE == 1)
  if (huart->Instance == USART3)
  {
    /* 读取UART接收数据寄存器中1Byte的数据 */
    UART3_Transfer.DMARxData[UART3_Transfer.DMARxData_Len] = USART3->RDR;
    UART3_Transfer.DMARxData_Len++;
    UART3_Transfer.Transfer_RX_Status = UART_TRANSFER_START;

    /* 接收到一包数据 */
    if (UART3_Transfer.DMARxData_Len == UART_RX_PACKAGE_SIZE)
    {
      UART3_Transfer.DMARxData_Len = 0;
      UART3_Transfer.Package_Data_RX_End_Flag = UART_TRANSFER_END;
    }

    /* 是否一帧数据过长 */
    if (UART3_Transfer.DMARxData_Len == UART_DATALEN_MAX - 1)
    {
      UART3_Transfer.DMARxData_Len = 0;
    }
  }
#endif

#if (STM32_UART4_ENABLE == 1)
  if (huart->Instance == UART4)
  {
    /* 读取UART接收数据寄存器中1Byte的数据 */
    UART4_Transfer.DMARxData[UART4_Transfer.DMARxData_Len] = UART4->RDR;
    UART4_Transfer.DMARxData_Len++;
    UART4_Transfer.Transfer_RX_Status = UART_TRANSFER_START;

    /* 接收到一包数据 */
    if (UART4_Transfer.DMARxData_Len == UART_RX_PACKAGE_SIZE)
    {
      UART4_Transfer.DMARxData_Len = 0;
      UART4_Transfer.Package_Data_RX_End_Flag = UART_TRANSFER_END;
    }

    /* 是否一帧数据过长 */
    if (UART4_Transfer.DMARxData_Len == UART_DATALEN_MAX - 1)
    {
      UART4_Transfer.DMARxData_Len = 0;
    }
  }
#endif

#if (STM32_UART5_ENABLE == 1)
  if (huart->Instance == UART5)
  {
    /* 读取UART接收数据寄存器中1Byte的数据 */
    UART5_Transfer.DMARxData[UART5_Transfer.DMARxData_Len] = UART5->RDR;
    UART5_Transfer.DMARxData_Len++;
    UART5_Transfer.Transfer_RX_Status = UART_TRANSFER_START;

    /* 接收到一包数据 */
    if (UART5_Transfer.DMARxData_Len == UART_RX_PACKAGE_SIZE)
    {
      UART5_Transfer.DMARxData_Len = 0;
      UART5_Transfer.Package_Data_RX_End_Flag = UART_TRANSFER_END;
    }

    /* 是否一帧数据过长 */
    if (UART5_Transfer.DMARxData_Len == UART_DATALEN_MAX - 1)
    {
      UART5_Transfer.DMARxData_Len = 0;
    }
  }
#endif
	
#if (STM32_UART6_ENABLE == 1)
  if (huart->Instance == USART6)
  {
    /* 读取UART接收数据寄存器中1Byte的数据 */
    UART6_Transfer.DMARxData[UART6_Transfer.DMARxData_Len] = USART6->RDR;
    UART6_Transfer.DMARxData_Len++;
    UART6_Transfer.Transfer_RX_Status = UART_TRANSFER_START;

    /* 接收到一包数据 */
    if (UART6_Transfer.DMARxData_Len == UART_RX_PACKAGE_SIZE)
    {
      UART6_Transfer.DMARxData_Len = 0;
      UART6_Transfer.Package_Data_RX_End_Flag = UART_TRANSFER_END;
    }

    /* 是否一帧数据过长 */
    if (UART6_Transfer.DMARxData_Len == UART_DATALEN_MAX - 1)
    {
      UART6_Transfer.DMARxData_Len = 0;
    }
  }
#endif
	
#if (STM32_UART7_ENABLE == 1)
  if (huart->Instance == UART7)
  {
    /* 读取UART接收数据寄存器中1Byte的数据 */
    UART7_Transfer.DMARxData[UART7_Transfer.DMARxData_Len] = UART7->RDR;
    UART7_Transfer.DMARxData_Len++;
    UART7_Transfer.Transfer_RX_Status = UART_TRANSFER_START;

    /* 接收到一包数据 */
    if (UART7_Transfer.DMARxData_Len == UART_RX_PACKAGE_SIZE)
    {
      UART7_Transfer.DMARxData_Len = 0;
      UART7_Transfer.Package_Data_RX_End_Flag = UART_TRANSFER_END;
    }

    /* 是否一帧数据过长 */
    if (UART7_Transfer.DMARxData_Len == UART_DATALEN_MAX - 1)
    {
      UART7_Transfer.DMARxData_Len = 0;
    }
  }
#endif
	
#if (STM32_UART8_ENABLE == 1)
  if (huart->Instance == UART8)
  {
    /* 读取UART接收数据寄存器中1Byte的数据 */
    UART8_Transfer.DMARxData[UART8_Transfer.DMARxData_Len] = UART8->RDR;
    UART8_Transfer.DMARxData_Len++;
    UART8_Transfer.Transfer_RX_Status = UART_TRANSFER_START;

    /* 接收到一包数据 */
    if (UART8_Transfer.DMARxData_Len == UART_RX_PACKAGE_SIZE)
    {
      UART8_Transfer.DMARxData_Len = 0;
      UART8_Transfer.Package_Data_RX_End_Flag = UART_TRANSFER_END;
    }

    /* 是否一帧数据过长 */
    if (UART8_Transfer.DMARxData_Len == UART_DATALEN_MAX - 1)
    {
      UART8_Transfer.DMARxData_Len = 0;
    }
  }
#endif
  
#if (UART_RECEIVE_FRAME_MODE == USE_TIM_UPDATE_RECEIVE_UART_FRAME)

#if (STM32_LPUART1_ENABLE == 1)
  if (huart->Instance == LPUART1)
  {
    /* TIM状态忙 */
    TIM_Status_LPUART1_RX = TIM_STATUS_BUSY;
    
    /* TIM计数器归0，重新计时 */
    __HAL_TIM_SET_COUNTER(&TIM_HANDLE_LPUART1_RX, 0);
      
    /* 使能定时器 */
    __HAL_TIM_ENABLE(&TIM_HANDLE_LPUART1_RX);
  }
#endif
  
#if (STM32_UART1_ENABLE == 1)
  if (huart->Instance == USART1)
  {
    /* TIM状态忙 */
    TIM_Status_UART1_RX = TIM_STATUS_BUSY;
    
    /* TIM计数器归0，重新计时 */
    __HAL_TIM_SET_COUNTER(&TIM_HANDLE_UART1_RX, 0);
      
    /* 使能定时器 */
    __HAL_TIM_ENABLE(&TIM_HANDLE_UART1_RX);
  }
#endif
  
#if (STM32_UART2_ENABLE == 1)
  if (huart->Instance == USART2)
  {
    /* TIM状态忙 */
    TIM_Status_UART2_RX = TIM_STATUS_BUSY;
    
    /* TIM计数器归0，重新计时 */
    __HAL_TIM_SET_COUNTER(&TIM_HANDLE_UART2_RX, 0);
      
    /* 使能定时器 */
    __HAL_TIM_ENABLE(&TIM_HANDLE_UART2_RX);
  }
#endif
  
#if (STM32_UART3_ENABLE == 1)
  if (huart->Instance == USART3)
  {
    /* TIM状态忙 */
    TIM_Status_UART3_RX = TIM_STATUS_BUSY;
    
    /* TIM计数器归0，重新计时 */
    __HAL_TIM_SET_COUNTER(&TIM_HANDLE_UART3_RX, 0);
      
    /* 使能定时器 */
    __HAL_TIM_ENABLE(&TIM_HANDLE_UART3_RX);
  }
#endif
  
#if (STM32_UART4_ENABLE == 1)
  if (huart->Instance == UART4)
  {
    /* TIM状态忙 */
    TIM_Status_UART4_RX = TIM_STATUS_BUSY;
    
    /* TIM计数器归0，重新计时 */
    __HAL_TIM_SET_COUNTER(&TIM_HANDLE_UART4_RX, 0);
      
    /* 使能定时器 */
    __HAL_TIM_ENABLE(&TIM_HANDLE_UART4_RX);
  }
#endif
  
#if (STM32_UART5_ENABLE == 1)
  if (huart->Instance == UART5)
  {
    /* TIM状态忙 */
    TIM_Status_UART5_RX = TIM_STATUS_BUSY;
    
    /* TIM计数器归0，重新计时 */
    __HAL_TIM_SET_COUNTER(&TIM_HANDLE_UART5_RX, 0);
      
    /* 使能定时器 */
    __HAL_TIM_ENABLE(&TIM_HANDLE_UART5_RX);
  }
#endif

#if (STM32_UART6_ENABLE == 1)
  if (huart->Instance == USART6)
  {
    /* TIM状态忙 */
    TIM_Status_UART6_RX = TIM_STATUS_BUSY;
    
    /* TIM计数器归0，重新计时 */
    __HAL_TIM_SET_COUNTER(&TIM_HANDLE_UART6_RX, 0);
      
    /* 使能定时器 */
    __HAL_TIM_ENABLE(&TIM_HANDLE_UART6_RX);
  }
#endif
  
#if (STM32_UART7_ENABLE == 1)
  if (huart->Instance == UART7)
  {
    /* TIM状态忙 */
    TIM_Status_UART7_RX = TIM_STATUS_BUSY;
    
    /* TIM计数器归0，重新计时 */
    __HAL_TIM_SET_COUNTER(&TIM_HANDLE_UART7_RX, 0);
      
    /* 使能定时器 */
    __HAL_TIM_ENABLE(&TIM_HANDLE_UART7_RX);
  }
#endif
  
#if (STM32_UART8_ENABLE == 1)
  if (huart->Instance == UART8)
  {
    /* TIM状态忙 */
    TIM_Status_UART8_RX = TIM_STATUS_BUSY;
    
    /* TIM计数器归0，重新计时 */
    __HAL_TIM_SET_COUNTER(&TIM_HANDLE_UART8_RX, 0);
      
    /* 使能定时器 */
    __HAL_TIM_ENABLE(&TIM_HANDLE_UART8_RX);
  }
#endif

#endif

  /* 串口接收中断 */
  __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
  
  __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_RXNE);

#if (UART_RECEIVE_FRAME_MODE == USE_UART_IDLE_RECEIVE_UART_FRAME)
  /* 串口空闲中断 */
  __HAL_UART_ENABLE_IT(huart, UART_IT_IDLE);
  
  __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_IDLE);
#endif

}


/**
  * @brief  UART使用空闲中断接收完成中断回调函数
  * @note   该函数由STM32_UART_RxCpltCallback调用
  * @param  huart: UART_HandleTypeDef
  * @retval 无
  */
void STM32_UART_RxCpltCallback_IDLE(UART_HandleTypeDef *huart)
{
#if (STM32_LPUART1_ENABLE == 1)
  if (huart->Instance == LPUART1)
  {
    memcpy(LPUART1_Transfer.RxData, LPUART1_Transfer.DMARxData, LPUART1_Transfer.DMARxData_Len);
    LPUART1_Transfer.RxData_Len = LPUART1_Transfer.DMARxData_Len;
        
    memset(LPUART1_Transfer.DMARxData, 0, UART_DATALEN_MAX);
    LPUART1_Transfer.DMARxData_Len = 0;

    /* 串口接收完成标志 */
    LPUART1_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
  }
#endif
  
#if (STM32_UART1_ENABLE == 1)
  if (huart->Instance == USART1)
  {
    memcpy(UART1_Transfer.RxData, UART1_Transfer.DMARxData, UART1_Transfer.DMARxData_Len);
    UART1_Transfer.RxData_Len = UART1_Transfer.DMARxData_Len;
        
    memset(UART1_Transfer.DMARxData, 0, UART_DATALEN_MAX);
    UART1_Transfer.DMARxData_Len = 0;

    /* 串口接收完成标志 */
    UART1_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
  }
#endif
  
#if (STM32_UART2_ENABLE == 1)
  if (huart->Instance == USART2)
  {
    memcpy(UART2_Transfer.RxData, UART2_Transfer.DMARxData, UART2_Transfer.DMARxData_Len);
    UART2_Transfer.RxData_Len = UART2_Transfer.DMARxData_Len;
        
    memset(UART2_Transfer.DMARxData, 0, UART_DATALEN_MAX);
    UART2_Transfer.DMARxData_Len = 0;

    /* 串口接收完成标志 */
    UART2_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
  }
#endif
  
#if (STM32_UART3_ENABLE == 1)
  if (huart->Instance == USART3)
  {
    memcpy(UART3_Transfer.RxData, UART3_Transfer.DMARxData, UART3_Transfer.DMARxData_Len);
    UART3_Transfer.RxData_Len = UART3_Transfer.DMARxData_Len;
        
    memset(UART3_Transfer.DMARxData, 0, UART_DATALEN_MAX);
    UART3_Transfer.DMARxData_Len = 0;

    /* 串口接收完成标志 */
    UART3_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
  }
#endif
  
#if (STM32_UART4_ENABLE == 1)
  if (huart->Instance == UART4)
  {
    memcpy(UART4_Transfer.RxData, UART4_Transfer.DMARxData, UART4_Transfer.DMARxData_Len);
    UART4_Transfer.RxData_Len = UART4_Transfer.DMARxData_Len;
        
    memset(UART4_Transfer.DMARxData, 0, UART_DATALEN_MAX);
    UART4_Transfer.DMARxData_Len = 0;
    
    /* 串口接收完成标志 */
    UART4_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
  }
#endif
  
#if (STM32_UART5_ENABLE == 1)
  if (huart->Instance == UART5)
  {
    memcpy(UART5_Transfer.RxData, UART5_Transfer.DMARxData, UART5_Transfer.DMARxData_Len);
    UART5_Transfer.RxData_Len = UART5_Transfer.DMARxData_Len;
        
    memset(UART5_Transfer.DMARxData, 0, UART_DATALEN_MAX);
    UART5_Transfer.DMARxData_Len = 0;
    
    /* 串口接收完成标志 */
    UART5_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
  }
#endif

#if (STM32_UART6_ENABLE == 1)
  if (huart->Instance == USART6)
  {
    memcpy(UART6_Transfer.RxData, UART6_Transfer.DMARxData, UART6_Transfer.DMARxData_Len);
    UART6_Transfer.RxData_Len = UART6_Transfer.DMARxData_Len;
        
    memset(UART6_Transfer.DMARxData, 0, UART_DATALEN_MAX);
    UART6_Transfer.DMARxData_Len = 0;
  
    /* 串口接收完成标志 */
    UART6_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
  }
#endif
  
#if (STM32_UART7_ENABLE == 1)
  if (huart->Instance == UART7)
  {
    memcpy(UART7_Transfer.RxData, UART7_Transfer.DMARxData, UART7_Transfer.DMARxData_Len);
    UART7_Transfer.RxData_Len = UART7_Transfer.DMARxData_Len;
        
    memset(UART7_Transfer.DMARxData, 0, UART_DATALEN_MAX);
    UART7_Transfer.DMARxData_Len = 0;
    
    /* 串口接收完成标志 */
    UART7_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
  }
#endif
  
#if (STM32_UART8_ENABLE == 1)
  if (huart->Instance == UART8)
  {
    memcpy(UART8_Transfer.RxData, UART8_Transfer.DMARxData, UART8_Transfer.DMARxData_Len);
    UART8_Transfer.RxData_Len = UART8_Transfer.DMARxData_Len;
        
    memset(UART8_Transfer.DMARxData, 0, UART_DATALEN_MAX);
    UART8_Transfer.DMARxData_Len = 0;
  
    /* 串口接收完成标志 */
    UART8_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
  }
#endif
  
  __HAL_UART_ENABLE_IT(huart, UART_IT_IDLE);
  
  /* 空闲中断需手动清理标志位 */
  __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_IDLE);
}


/**
  * @brief  UART发送完成中断回调函数
  * @note   该函数由UART_IRQHandler调用
  * @param  huart: UART_HandleTypeDef
  * @retval 无
  */
void STM32_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
  ;
}


#ifdef CUSTOM_PRINTF_UART

#ifdef IAR_ARM_IDE

#include <LowLevelIOInterface.h>

#pragma module_name = "?__write"

int f_putchar(int ch)
{
  STM32_UART_TransmitData(&hlpuart1, (uint8_t *)&ch, 1);
  return ch;
}

/*
* If the __write implementation uses internal buffering, uncomment
* the following line to ensure that we are called with "buffer" as 0
* (i.e. flush) when the application terminates.
*/
size_t __write(int handle, const unsigned char * buffer, size_t size)
{
  /* Remove the #if #endif pair to enable the implementation */
#if 1
  size_t nChars = 0;

  if (buffer == 0)
  {
    /*
     * This means that we should flush internal buffers.  Since we
     * don't we just return.  (Remember, "handle" == -1 means that all
     * handles should be flushed.)
     */
    return 0;
  }

  /* This template only writes to "standard out" and "standard err",
   * for all other file handles it returns failure. */
  if (handle != _LLIO_STDOUT && handle != _LLIO_STDERR)
  {
    return _LLIO_ERROR;
  }

  for (/* Empty */; size != 0; --size)
  {
    if (f_putchar(*buffer++) < 0)
    {
      return _LLIO_ERROR;
    }
    ++nChars;
  }
  return nChars;

#else
  /* Always return error code when implementation is disabled. */
  return _LLIO_ERROR;
#endif
}

#else

/* 重定向printf函数到串口 */
int fputc(int ch, FILE *f)
{
  STM32_UART_TransmitData(&DEBUG_UART_HANDLE, (uint8_t *)&ch, 1);
	
  return ch;
}
#endif

#endif


